package com.example.rentingcockpit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.rentingcockpit.entity.Renting;
import com.example.rentingcockpit.mapper.RentingMapper;
import com.example.rentingcockpit.pojo.RadarAppliance;
import com.example.rentingcockpit.pojo.RadarFurniture;
import com.example.rentingcockpit.service.IRentingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import com.alibaba.fastjson.JSONArray;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Runaway
 * @since 2022-08-25
 */
@Service
public class RentingServiceImpl extends ServiceImpl<RentingMapper, Renting> implements IRentingService {
    @Autowired
    private RentingMapper rentingMapper;

    @Override
    public RadarAppliance radarApplianceProportionObject(String city) {
        QueryWrapper<Renting> query = new QueryWrapper<>();
        query.like("city", city);
        int total = rentingMapper.selectCount(query);
        query.clear();

        query.like("city", city).eq("tv", 1);
        int numTv = rentingMapper.selectCount(query);
        double portionTv = new BigDecimal(1.0 * numTv / total).setScale(2, RoundingMode.HALF_UP).doubleValue();
        query.clear();

        query.like("city", city).eq("refrigerator", 1);
        int numRefrigerator = rentingMapper.selectCount(query);
        double portionRefrigerator = new BigDecimal(1.0 * numRefrigerator / total).setScale(2, RoundingMode.HALF_UP).doubleValue();
        query.clear();

        query.like("city", city).eq("washer", 1);
        int numWasher = rentingMapper.selectCount(query);
        double portionWasher = new BigDecimal(1.0 * numWasher / total).setScale(2, RoundingMode.HALF_UP).doubleValue();
        query.clear();

        query.like("city", city).eq("aircondition", 1);
        int numAircondition = rentingMapper.selectCount(query);
        double portionAircondition = new BigDecimal(1.0 * numAircondition / total).setScale(2, RoundingMode.HALF_UP).doubleValue();
        query.clear();

        query.like("city", city).eq("waterheater", 1);
        int numWaterheater = rentingMapper.selectCount(query);
        double portionWaterheater = new BigDecimal(1.0 * numWaterheater / total).setScale(2, RoundingMode.HALF_UP).doubleValue();
        query.clear();

        query.like("city", city).eq("broadband", 1);
        int numBroadband = rentingMapper.selectCount(query);
        double portionBroadband = new BigDecimal(1.0 * numBroadband / total).setScale(2, RoundingMode.HALF_UP).doubleValue();
        query.clear();


        RadarAppliance radarAppliance = new RadarAppliance();
        radarAppliance.setTv(portionTv);
        radarAppliance.setRefrigerator(portionRefrigerator);
        radarAppliance.setWasher(portionWasher);
        radarAppliance.setAircondition(portionAircondition);
        radarAppliance.setWaterheater(portionWaterheater);
        radarAppliance.setBroadband(portionBroadband);
        return radarAppliance;
    }

    @Override
    public List<Double> radarApplianceProportion(String city) {
        ArrayList<Double> l = new ArrayList<>();
        RadarAppliance radarAppliance = radarApplianceProportionObject(city);

        l.add(radarAppliance.getTv());
        l.add(radarAppliance.getRefrigerator());
        l.add(radarAppliance.getWasher());
        l.add(radarAppliance.getAircondition());
        l.add(radarAppliance.getWaterheater());
        l.add(radarAppliance.getBroadband());

        return l;
    }

    @Override
    public RadarFurniture radarFurnitureProportionObject(String city) {
        QueryWrapper<Renting> query = new QueryWrapper<>();
        query.like("city", city);
        int total = rentingMapper.selectCount(query);
        query.clear();

        query.like("city", city).eq("balcony", 1);
        int numBalcony = rentingMapper.selectCount(query);
        double portionBalcony = new BigDecimal(1.0 * numBalcony / total).setScale(2, RoundingMode.HALF_UP).doubleValue();
        query.clear();

        query.like("city", city).eq("bed", 1);
        int numBed = rentingMapper.selectCount(query);
        double portionBed = new BigDecimal(1.0 * numBed / total).setScale(2, RoundingMode.HALF_UP).doubleValue();
        query.clear();

        query.like("city", city).eq("closet", 1);
        int numCloset = rentingMapper.selectCount(query);
        double portionCloset = new BigDecimal(1.0 * numCloset / total).setScale(2, RoundingMode.HALF_UP).doubleValue();
        query.clear();

        query.like("city", city).eq("sofa", 1);
        int numSofa = rentingMapper.selectCount(query);
        double portionSofa = new BigDecimal(1.0 * numSofa / total).setScale(2, RoundingMode.HALF_UP).doubleValue();
        query.clear();

        query.like("city", city).eq("gas", 1);
        int numGas = rentingMapper.selectCount(query);
        double portionGas = new BigDecimal(1.0 * numGas / total).setScale(2, RoundingMode.HALF_UP).doubleValue();
        query.clear();

        query.like("city", city).eq("radiator", 1);
        int numRadiator = rentingMapper.selectCount(query);
        double portionRadiator = new BigDecimal(1.0 * numRadiator / total).setScale(2, RoundingMode.HALF_UP).doubleValue();
        query.clear();


        RadarFurniture radarFurniture = new RadarFurniture();
        radarFurniture.setBalcony(portionBalcony);
        radarFurniture.setBed(portionBed);
        radarFurniture.setCloset(portionCloset);
        radarFurniture.setSofa(portionSofa);
        radarFurniture.setGas(portionGas);
        radarFurniture.setRadiator(portionRadiator);
        return radarFurniture;
    }

    @Override
    public List<Double> radarFurnitureProportion(String city) {
        ArrayList<Double> l = new ArrayList<>();
        RadarFurniture radarFurniture = radarFurnitureProportionObject(city);

        l.add(radarFurniture.getBalcony());
        l.add(radarFurniture.getBed());
        l.add(radarFurniture.getCloset());
        l.add(radarFurniture.getSofa());
        l.add(radarFurniture.getGas());
        l.add(radarFurniture.getRadiator());

        return l;
    }

    @Override
    public int sunburstDistrict(String city, String district) {
        QueryWrapper<Renting> query = new QueryWrapper<>();
        query.like("city", city);
        query.like("district", district);
        return rentingMapper.selectCount(query);
    }

    @Override
    public int digitalAvailability(String city) {
        QueryWrapper<Renting> query = new QueryWrapper<>();
        if (!Objects.equals(city, "全部")) {
            query.like("city", city);
        }
        return rentingMapper.selectCount(query);
    }

    @Override
    public List<List<Integer>> pieRoomProportion(String city) {
        List<Integer> listBedroom=new ArrayList<>();
        List<Integer> listBathroom=new ArrayList<>();
        List<Integer> listHall=new ArrayList<>();
        QueryWrapper<Renting> query = new QueryWrapper<>();
        query.eq("city", city).le("bedroom",1);
        listBedroom.add(rentingMapper.selectCount(query));
        query.clear();
        query.eq("city", city).eq("bedroom",2);
        listBedroom.add(rentingMapper.selectCount(query));
        query.clear();
        query.eq("city", city).ge("bedroom",3);
        listBedroom.add(rentingMapper.selectCount(query));
        query.clear();

        query.eq("city", city).eq("bathroom",0);
        listBathroom.add(rentingMapper.selectCount(query));
        query.clear();
        query.eq("city", city).eq("bathroom",1);
        listBathroom.add(rentingMapper.selectCount(query));
        query.clear();
        query.eq("city", city).eq("bathroom",2);
        listBathroom.add(rentingMapper.selectCount(query));
        query.clear();
        query.eq("city", city).ge("bathroom",3);
        listBathroom.add(rentingMapper.selectCount(query));
        query.clear();

        query.eq("city", city).eq("hall",0);
        listHall.add(rentingMapper.selectCount(query));
        query.clear();
        query.eq("city", city).eq("hall",1);
        listHall.add(rentingMapper.selectCount(query));
        query.clear();
        query.eq("city", city).eq("hall",2);
        listHall.add(rentingMapper.selectCount(query));
        query.clear();
        query.eq("city", city).ge("hall",3);
        listHall.add(rentingMapper.selectCount(query));
        query.clear();

        List<List<Integer>> l=new ArrayList<>();
        l.add(listBedroom);
        l.add(listBathroom);
        l.add(listHall);

        return l;
    }

    public HashMap barchartPrice() {
        ArrayList<String> cityList = new ArrayList();
        cityList.add("北京");
        cityList.add("上海");
        cityList.add("深圳");
        HashMap<String, Object> result = new HashMap<>();
        for (String city : cityList) {
            ArrayList<Integer> numList = new ArrayList<>();
            QueryWrapper<Renting> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("city", city).lt("price", 2000);
            numList.add(rentingMapper.selectCount(queryWrapper));
            queryWrapper.clear();
            queryWrapper.eq("city", city).between("price", 2000, 4000);
            numList.add(rentingMapper.selectCount(queryWrapper));
            queryWrapper.clear();
            queryWrapper.eq("city", city).between("price", 4000, 6000);
            numList.add(rentingMapper.selectCount(queryWrapper));
            queryWrapper.clear();
            queryWrapper.eq("city", city).between("price", 6000, 10000);
            numList.add(rentingMapper.selectCount(queryWrapper));
            queryWrapper.clear();
            queryWrapper.eq("city", city).gt("price", 10000);
            numList.add(rentingMapper.selectCount(queryWrapper));
            result.put(city, numList);
        }
        return result;
    }


    public List<Integer> pictogramFloor() {
        ArrayList<Integer> floorList = new ArrayList<>();
        QueryWrapper<Renting> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("level",1);
//        floorList.add(rentingMapper.selectCount(queryWrapper));
//        queryWrapper.clear();
        queryWrapper.eq("level", 2);
        floorList.add(rentingMapper.selectCount(queryWrapper));
        queryWrapper.clear();
        queryWrapper.eq("level", 3);
        floorList.add(rentingMapper.selectCount(queryWrapper));
        queryWrapper.clear();
        queryWrapper.eq("level", 4);
        floorList.add(rentingMapper.selectCount(queryWrapper));
        return floorList;
    }

    @Override
    public JSONObject mapLocation() {
        ArrayList<String> cityList = new ArrayList();
        cityList.add("北京");
        cityList.add("上海");
        cityList.add("深圳");
//        Map<String, ArrayList> maplist = new HashMap<>();
        JSONObject maplist=new JSONObject();
        for (String city : cityList) {
            QueryWrapper<Renting> queryWrapper = new QueryWrapper<>();
//            ArrayList<Map> communitylist = new ArrayList<>();
            JSONArray communitylist =new JSONArray();
            queryWrapper.eq("city", city);
            queryWrapper.select("community,lng,lat,count(*) as count");
            queryWrapper.groupBy("lng", "lat", "community");
            List<Map<String, Object>> list = rentingMapper.selectMaps(queryWrapper);
            for (Map<String, Object> map : list) {
//                ArrayList<Double> arrayList = new ArrayList<>();
                JSONArray arrayList = new JSONArray();
                arrayList.add(Double.parseDouble(map.get("lng").toString()));
                arrayList.add(Double.parseDouble(map.get("lat").toString()));
                arrayList.add(Double.parseDouble(map.get("count").toString()));
//                Map<String, ArrayList<Double>> cityMap = new HashMap<>();
                JSONObject cityMap =new JSONObject();
                cityMap.put("name",map.get("community").toString().replace("...", "").replace("(", "").replace(")", "").replace("?","").replace("-","").replace(",",""));
                cityMap.put("value", arrayList);
                communitylist.add(cityMap);
            }
            maplist.put(city, communitylist);
//            rentingMapper.selectList(queryWrapper);
        }
//        System.out.println(maplist);
        return maplist;
    }
}
